<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$setting=$this->plugin_setting('setting');
$member=$this->pdo_get('member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
$sale_mid=$_RQ['sale_mid'];
if($sale_mid){
	$update=array('sale_mid'=>$sale_mid);
	if($member['is_agent']){
		$share_link=$return['pluginset']['share_link']?:'jy_commission/index/';
		exi('您已是分销商！不能成为他人的直销用户！','error',$share_link);
	}else{
		$this->model('member')->member_update($_SESSION['uid'],$update);
	}
}
if($_RQ['footernav']){
	$return['pluginset']['footernav']=plugin($_RQ['footernav'])->plugin_setting('setting')['footernav'];
}
$mid=$_RQ['mid']?$_RQ['mid']:$_SESSION['uid'];
$sao_fromuser=$this->session('sao_fromuser');
if($_RQ['fromuser'] || $sao_fromuser){
		if($_RQ['fromuser'] && !$sale_mid && ($this->setting['become']==5 || $this->setting['become_child']==4)){
			$this->session('becomefromuser',$_RQ['fromuser']);
			$this->model('member')->checkmember('',false);
		}
		$_RQ['fromuser']=$_RQ['fromuser']?:$sao_fromuser;
		$team=$this->pdo_get('member',array('mid'=>$_RQ['fromuser'],'uniacid'=>$_SESSION['uniacid']));
		$teamlevel =$this->pdo_get('level',array('id'=>$team['level_id'],'uniacid'=>$_SESSION['uniacid']));
		$teamlevel['title'] = $teamlevel['title']?:'普通会员';
		//扫码成为下线
		if($_RQ['fromuser'] && ($this->setting['become_child']==4 || $this->setting['become_child']==1) && !$member['agent_id']){
			$this->model('member')->set_parent($_SESSION['uid'],$_RQ['fromuser']);
		}
		if($teamlevel['isyidong']==1 && $_RQ['fromuser'] && $member['agent_id']){
			$this->model('member')->set_parent($_SESSION['uid'],$_RQ['fromuser'],true);
		}
		if($teamlevel['set_group'] && $_RQ['fromuser']){
			model('jy_weishop/member')->up_member_level($_SESSION['uid'],$teamlevel['set_group']);
			if($teamlevel['set_feelevelid']){
				$this->update('member',array('level_id'=>$teamlevel['set_feelevelid']),array('mid'=>$_SESSION['uid']));
			}
		}
		$this->session('sao_fromuser','');
}
if(($_RQ['fromuser'] || $sale_mid) && $return['pluginset']['share_link']){
	exi('','',$return['pluginset']['share_link']);
}
if($_SESSION['current_plugin']!='jy_commission'){
	$nowplugin=$_SESSION['current_plugin']=='core'?'jy_weishop':$_SESSION['current_plugin'];
	$menus_index=$_SESSION['menus_index']?:$nowplugin.'/index/';
	//exi('','',$menus_index);
}
if($return['pluginset']['myshop']){
	$share_link=$return['pluginset']['share_link']?:$menus_index;
	exi('','',$share_link);
}
if($_RQ['op']=='main'){
	$myshop=pdo_get('jy_commission_shop',array('mid'=>$sale_mid?:$mid,'uniacid'=>$_SESSION['uniacid']));
	if(!$myshop['mid']){
		$res=$this->model('member')->check_myshop($mid);
		if(!$res){
			//exi('该用户还没有小店！','error','jy_weishop/index/');
		}
	}
	$return['mid']=$mid;
	$return['sharedata']=array(
			'title'=>$myshop['title']?$myshop['title']:'我的小店',
			'desc'=>$myshop['desc']?:$myshop['title'].'描述',
			'link'=>custom_url('jy_commission/myshop/main/mid='.$mid,true),
			'imgUrl'=>tomedia($myshop['logo']),
	);
	$return['member']=pdo_get('core_members',array('id'=>$myshop['mid'],'uniacid'=>$_SESSION['uniacid']),array('id','avatar','nickname'));
	$myshop['logo']=$myshop['logo']?$myshop['logo']:$return['member']['avatar'];
	$return['setting']=$setting;

	$level=pdo_getcolumn('jy_commission_member',array('mid'=>$_SESSION['uid']),'level_id');
	if($level){
		$return['level']=pdo_getcolumn('jy_commission_level',array('id'=>$level),'title');	
	}else{
		$return['level']='普通等级';
	}
	$return['ordercount']=$total['total'];
	$goodsids = explode(';',$myshop['goodsids']);
	if($myshop['selectgoods']){
		$goodsids = explode(';',$myshop['goodsids']);
		$params['id']=$goodsids;
		$return['goodscount']=count($goodsids)-2;
	}else{
		$return['goodscount']=pdo_count('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'status'=>1));
	}
	$myshop['title'] = $myshop['title']?$myshop['title']:$return['member']['nickname'].'的小店';
	$myshop['logo'] = $myshop['logo']?tomedia($myshop['logo']):$return['member']['avatar'];
	$myshop['img'] = $myshop['img']?tomedia($myshop['img']):'';
	$image=ROOT_AT.'images/jy_commission/'.$_SESSION['uniacid'].'/id'.$mid.'.jpg';
	if(!file_exists($image)){
		$url='/jy_commission/myshop/index?mid='.$mid.'&fromuser='.$_SESSION['uid'];
		$wxapp=pdo_get('core_account_wxapp',array('id'=>$_SESSION['wxapp_id'],'uniacid'=>$_SESSION['uniacid']));
		$res=model('core/wxapp')->appcode($url,$wxapp['appid'],$wxapp['appsecret'],200);
		$path=ROOT_AT.'images/jy_commission/'.$_SESSION['uniacid'];
		if(!file_exists($path)){
				mkdir($path,0700, true);
		}
		file_put_contents($path.'/id'.$mid.'.jpg',$res);
	}
	$myshop['appcode'] = 'images/jy_commission/'.$_SESSION['uniacid'].'/id'.$mid.'.jpg';
	$myshop['title1']=cfc('tools')->substr($myshop['title'],0,13);
	$myshop['title2']=cfc('tools')->substr($myshop['desc'],0,13);
	$myshop['title2']=$myshop['title2']?$myshop['title2'].'...':'暂无描述';
	$return['myshop']=$myshop;
	$return['cartCount'] = $_SESSION['uid']?pdo_getsum('jy_weishop_member_cart', array('mid' => $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total'):0;
	$return['params']=array('page'=>1);
	set_html('title',$myshop['title']);
	exi($return);
}
if($_RQ['op']=='list'){
	$myshop=pdo_get('jy_commission_shop',array('mid'=>$sale_mid?:$mid,'uniacid'=>$_SESSION['uniacid']));
	$return['params']=array();
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1);
	if($myshop['selectgoods']){
		$goodsids = explode(';',$myshop['goodsids']);
		$params['id']=$goodsids;
	}
	if($_RQ['keyword']){
		if(is_numeric($_RQ['keyword'])){
			$params['id']=intval($_RQ['keyword']);
			$return['params']['id']=intval($_RQ['keyword']);
		}else{
			$params['title like']='%'.trim($_RQ['keyword']).'%';
			$return['params']['title']=$_RQ['keyword'];
		}
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 20;
	if($_RQ['num']){
		$psize=$_RQ['num'];
	}
	$parameter=array(
			'params'=>$params,
			'limit'=>array($pindex,$psize),
			'fields'=>$fields,
			'orderby'=>$orderby,
	);
	$list=model('jy_weishop/goods')->goods_list($parameter,$total);
	if(!$list){
		exi('none');
	}
	if($list){
		$install = check_permission('jy_merch');
		foreach($list as &$itemv){
			$itemv['originalprice']=$itemv['originalprice']>0?$itemv['originalprice']:price($itemv['price']*1.2);
			$itemv['createtime']=$itemv['createtime']?:time()-58*3600*24;
			$itemv['createtime']=date('Y-m-d',$itemv['createtime']);
			//按点赞数排序作品
			$pindex = max(1, 0);
			$psize = 4;
			$members=pdo_getslice('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$itemv['id']),array($pindex,$psize),$total,array('mid'),'','id desc');
			foreach($members as &$mv){
				$memberinfo=pdo_get('core_members',array('id'=>$mv['mid']),array('nickname','avatar'));
				$mv['nickname']=$memberinfo['nickname'];
				$mv['avatar']=$memberinfo['avatar'];
			}
			$itemv['members']=$members;
			
			if($install){
				$itemv['merchname']=pdo_getcolumn('jy_merch_user',array('id'=>$itemv['merchid']),'merchname');
			}
			$itemv['likes']=pdo_count('jy_weishop_member_history',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$itemv['id'],),'id');
			$itemv['carttotal']=pdo_getcolumn('jy_weishop_member_cart',array('goodsid'=>$itemv['id'],'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'total');
			$itemv['cartid']=pdo_getcolumn('jy_weishop_member_cart',array('goodsid'=>$itemv['id'],'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'id');
			$itemv['cartid']=$itemv['cartid']?:0;
			$itemv['carttotal']=$itemv['carttotal']?:0;
			//规格
				$specs_list=pdo_getall('jy_weishop_goods_spec',array('goodsid'=>$itemv['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
				//已选规格
				$selectspecs = array();
				$selespename = '';
				if($specs_list){
						foreach($specs_list as $k=>&$sp){
							$sp['items']=pdo_getall('jy_weishop_goods_spec_item',array('specid'=>$sp['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
							$selectspecs[$k]=$sp['items'][0]['id'];
							$selespename .=$sp['items'][0]['title'].'，';
						}
				}
				//默认已选参数：
				$spsids = '';
				foreach($selectspecs as $k=>&$s){
						if($k==0){
							$spsids .= $s;
						}else{
							$spsids .= '_'.$s;
						}
				}
				if($store_id){
					$getinfo=model('jy_weishop/store')->getinfo($itemv['id'],$store_id);
					$itemv['total']=$getinfo['id']?$getinfo['total']:$itemv['total'];
					$itemv['price']=$getinfo['id']?$getinfo['price']:$itemv['price'];
					$itemv['options']=model('jy_weishop/store')->getoption($itemv['id'],$store_id,$spsids);
				}
				if(!$itemv['options']['id']){
					$itemv['options'] = pdo_get('jy_weishop_goods_option',array('goodsid'=>$itemv['id'],'specs'=>$spsids));
				}
				//默认商品规格价格：
				if($itemv['options']){
					$itemv['price']=$itemv['options']['price'];
					if($itemv['active']['discount']>10){
						$itemv['options']['oldprice']=$itemv['options']['price'];
						$itemv['price']=$itemv['options']['oldprice']*$itemv['active']['discount']/100;
						$itemv['options']['price']=$itemv['price'];
					}
					$cartnum=pdo_getcolumn('jy_weishop_member_cart',array('selected'=>1,'optionid'=>$itemv['options']['id'],'goodsid'=>$itemv['id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total');
					$itemv['post'][$itemv['options']['id']]=array('num'=>$cartnum?:0,'title'=>$selespename);
				}
				$itemv['selespename'] = $selespename;
				$itemv['specs_list'] = $specs_list;
				$itemv['specid']=$selectspecs;
				$itemv['optionid']=$itemv['options']['id'];
				$itemv['image']=$itemv['image']?tomedia($itemv['image']):'';
		}
	}
	exi($list);
}
?>